package com.jake.feeddemo;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jake.xfeed.recyclerfeed.BaseCard;
import com.jake.xfeed.recyclerfeed.BaseFeedDiffCallback;
import com.jake.xfeed.recyclerfeed.CardTypeConverter;
import com.jake.xfeed.recyclerfeed.HeaderFooterContainer;
import com.jake.xfeed.recyclerfeed.HeaderFooterFeedAdapter;
import com.jake.xfeed.recyclerfeed.ICardFactory;
import com.jake.xfeed.recyclerfeed.IDataSource;
import com.jake.xfeed.recyclerfeed.RecyclerViewDiffUtils;
import com.jake.xfeed.recyclerfeed.SingleFooterFeedAdapter;

import java.util.ArrayList;
import java.util.List;

public class FeedWidget extends FrameLayout {

    private HeaderFooterFeedAdapter<CardData> mAdapter;
    private List<CardData> mDataList = new ArrayList<>();

    public FeedWidget(@NonNull Context context) {
        super(context);
        initContent();
    }

    private void initContent() {
        RecyclerView recyclerView = new RecyclerView(this.getContext());
        LinearLayoutManager manager = new LinearLayoutManager(this.getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        this.addView(recyclerView, -1, -1);
        mAdapter = new HeaderFooterFeedAdapter<>(new IDataSource<CardData>() {

            @Override
            public CardData getItem(int position) {
                return mDataList.get(position);
            }

            @Override
            public int getItemPosition(CardData cardData) {
                return mDataList.indexOf(cardData);
            }

            @Override
            public int getItemCount() {
                return mDataList.size();
            }
        });
        mAdapter.setCardTypeConverter(new CardTypeConverter<CardData>() {
            @Override
            public int getType(int position, @NonNull CardData cardData) {
                return 0;
            }

        });
        mAdapter.setCardFactory(new ICardFactory() {
            @Override
            public BaseCard<CardData> getCard(@NonNull Context context, int cardType) {
                return new TextCard(context);
            }
        });
//        mAdapter.setFooterView(getLoadMoreView());
        for (int i = index; i < 10; i++) {
            CardData cardData = new CardData();
            cardData.title = "初始数据  " + i;
            mDataList.add(cardData);
        }
//        recyclerView.setAdapter(mAdapter);
        SingleFooterFeedAdapter<LoadMoreView> loadMoreAdapter = new SingleFooterFeedAdapter<>(getLoadMoreView());
        loadMoreAdapter.setObserver(new SingleFooterFeedAdapter.ObserverAdapter<LoadMoreView>() {
            @Override
            public void onViewAttachedToWindow(@NonNull LoadMoreView loadMoreView) {
                loadMoreView.onCardAttachedToWindow();
            }
        });
        ConcatAdapter adapter = new ConcatAdapter(ConcatAdapter.Config.DEFAULT, List.of(mAdapter, loadMoreAdapter));
        recyclerView.setAdapter(adapter);
    }

    private @NonNull LoadMoreView getLoadMoreView() {
        LoadMoreView loadMoreView = new LoadMoreView(this.getContext());
        loadMoreView.setOnLoadMoreListener(new LoadMoreView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                postDelayed(FeedWidget.this::loadMoreTask, 500);
            }
        });
        return loadMoreView;
    }

    private void loadMoreTask() {
        List<CardData> last = new ArrayList<>(mDataList);
        int len = 5;
        for (int i = index; i < index + len; i++) {
            CardData cardData = new CardData();
            cardData.title = "加载更多  " + i;
            Log.d("jake", "onLoadMore  for i=" + i);
            mDataList.add(cardData);
        }
        index += len;
        RecyclerViewDiffUtils.updateData(new MyDiffCallback(last, mDataList), mAdapter.getListUpdateCallback());
    }

    int index = 0;

    private static class CardData {
        String title;
    }

    private static class MyDiffCallback extends BaseFeedDiffCallback<CardData> {

        public MyDiffCallback(@NonNull List<CardData> oldList, @NonNull List<CardData> newList) {
            super(oldList, newList);
        }

        @Override
        protected boolean areItemsTheSame(int oldItemPosition, @NonNull CardData oldItem, int newItemPosition, @NonNull CardData newItem) {
            return TextUtils.equals(oldItem.title, newItem.title);
        }

        @Override
        protected boolean areContentsTheSame(int oldItemPosition, @NonNull CardData oldItem, int newItemPosition, @NonNull CardData newItem) {
            return TextUtils.equals(oldItem.title, newItem.title);
        }
    }

    private static class LoadMoreView extends HeaderFooterContainer {
        TextView textView;
        private OnLoadMoreListener mOnLoadMoreListener;

        public LoadMoreView(@NonNull Context context) {
            super(context);
            textView = new TextView(getContext());
            textView.setTextColor(0xff333333);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(30, 50, 30, 50);
            textView.setText("正在努力加载......");
            this.addView(textView, -1, -1);
            textView.setOnClickListener(v -> loadMore());
        }

        @Override
        public void onCardAttachedToWindow() {
            super.onCardAttachedToWindow();
            post(this::loadMore);
        }

        private void loadMore() {
            if (mOnLoadMoreListener != null) {
                mOnLoadMoreListener.onLoadMore();
            }
        }

        private void setOnLoadMoreListener(OnLoadMoreListener listener) {
            mOnLoadMoreListener = listener;
        }

        public interface OnLoadMoreListener {
            void onLoadMore();
        }
    }

    private static class TextCard extends BaseCard<CardData> {
        TextView textView;

        public TextCard(@NonNull Context context) {
            super(context);
            textView = new TextView(getContext());
            textView.setTextColor(0xff333333);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(30, 50, 30, 50);
            this.addView(textView, -1, -1);
        }

        @Override
        protected void onBind(int position, CardData cardData) {
            textView.setText(cardData.title);
        }
    }
}
